package com.chendx.store.user.netty;

import com.chendx.store.user.config.ApplicationContextProvider;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by chendx on 2022/5/5.
 */
public class NoticeCountWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {


    private static ObjectMapper MAPPER = new ObjectMapper();
    //获取WebSocket连接的map
    public static ConcurrentHashMap<String, Channel> webSocketMap = new ConcurrentHashMap<>();

    @Autowired
    private RabbitTemplate rabbitTemplate;

   // private static RabbitTemplate rabbitTemplate = ApplicationContextProvider.getApplicationContext().getBean(RabbitTemplate.class);

    private static StringRedisTemplate redisTemplate = ApplicationContextProvider.getApplicationContext().getBean(StringRedisTemplate.class);

    private SimpleMessageListenerContainer customerNoticeContainer =
            (SimpleMessageListenerContainer) ApplicationContextProvider.getApplicationContext().getBean("customerNoticeContainer");

    @Override
    protected void channelRead0(ChannelHandlerContext ctx,  TextWebSocketFrame message) throws Exception {
        String json = message.text();
        String username = MAPPER.readTree(json).get("username").asText();
        String customer = MAPPER.readTree(json).get("customer").asText();
        Channel channel = webSocketMap.get(username);
    }
}
